# -*- coding: utf-8 -*-            
# @Author : chenliang
# @Time : 2024/3/7 16:23
import time
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_bill_locator import AdminBillLocator
from Case.python_ui.Test_data.menu_name_data import MenuName
import re
from Case.python_ui.Common.excel import excel
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.PageLocate.admin_ticket_management_locator import AdminTicketManagementLocator
from Case.python_ui.Common.business_common_tool import BusinessCommonTools


class AdminBill(Basepage):

    """
        公共方法
    """
    # 判断列表降序
    def is_descending_order(self,float_list):
        for i in range(len(float_list) - 1):
            if not isinstance(float_list[i], float):
                return False  # 如果列表中有非浮点数，则直接返回False
            if float_list[i] <= float_list[i + 1]:
                return False  # 如果有任何一对相邻元素不是降序排列，则返回False
        return True  # 如果所有相邻元素都是降序排列，则返回True

    # 判断列表升序
    def is_ascending_order(self,float_list):
        for i in range(len(float_list) - 1):
            if not isinstance(float_list[i], float):
                return False  # 如果列表中有非浮点数，则直接返回False
            if float_list[i] >= float_list[i + 1]:
                return False  # 如果有任何一对相邻元素不是升序排列，则返回False
        return True  # 如果所有相邻元素都是升序排列，则返回True


    """
        Bill - Income/Expense
    """

    # 等待页面加载转动
    def wait_web_page_rotate(self,time=100):
        doc = '等待页面加载转动图标'
        try:
            self.wait_elevisble(AdminBillLocator.wait, doc,time)
            self.wait_eleinvisble(AdminBillLocator.wait, doc,time)
        except:
            pass

    # 进入Bill Income/Expense页面
    def enter_bill_IncomeExpense(self):
        doc = '进入Bill Income/Expense页面'
        time.sleep(5)
        self.scroll_bar_operation(AdminBillLocator.bill_menu)
        text=self.get_attribute(AdminBillLocator.if_open_bill_menu,'Class',doc)
        if text != 'antd4-menu antd4-menu-sub antd4-menu-inline':
            self.move_mouse_to_click_element(AdminBillLocator.bill_menu,doc)
        self.wait_elevisble(AdminBillLocator.incomeExpense_menu,doc)
        self.move_mouse_to_click_element(AdminBillLocator.incomeExpense_menu,doc)
        self.wait_web_page_rotate(time=10)
        self.wait_elevisble(AdminBillLocator.incomeExpense_page_text,doc)
        assert self.get_element(AdminBillLocator.incomeExpense_page_text,doc).text == 'Income/Expense'


    # 清空时间选项，选择当前时间
    def clear_time_select(self, if_now='1'):
        doc = '清空时间选项，选择当前时间'
        time.sleep(2)
        try:
            self.move_mouse_to_click_element(AdminBillLocator.clear_time, doc)
        except:
            pass
        try:
            self.wait_elevisble(AdminBillLocator.wait, doc, timeout=10)
            self.wait_eleinvisble(AdminBillLocator.wait, doc, timeout=10)
        except:
            pass
        if if_now == '1':
            self.wait_elevisble(AdminBillLocator.order_time, doc)
            self.click_element(AdminBillLocator.order_time, doc)
            self.wait_elevisble(AdminBillLocator.order_select_time, doc)
            self.click_element(AdminBillLocator.order_select_time, doc)
            self.click_element(AdminBillLocator.order_select_time, doc)
        if if_now == '2':
            self.wait_elevisble(AdminBillLocator.order_time, doc)
            self.click_element(AdminBillLocator.order_time, doc)
            self.wait_elevisble(AdminBillLocator.order_select_time_yesterday, doc)
            self.click_element(AdminBillLocator.order_select_time_yesterday, doc)
            self.click_element(AdminBillLocator.order_select_time_yesterday, doc)
        time.sleep(5)
        self.save_screen(doc)

    # 清空时间选项，点击下载
    def clear_time_download(self):
        doc = '清空时间选项，点击下载'
        self.clear_time_select(if_now='3')
        self.wait_web_page_rotate(time=10)
        self.wait_elevisble(AdminBillLocator.click_download,doc)
        self.move_mouse_to_click_element(AdminBillLocator.click_download,doc)
        self.wait_elevisble(AdminBillLocator.download_point_out,doc)
        self.wait_eleinvisble(AdminBillLocator.download_point_out,doc)

    #清空时间选项，选择特定时间，返回数量
    def clear_time_select_choose_time(self,cmdopt):
        doc = '清空时间选项，选择特定时间'
        time.sleep(2)
        try:
            self.move_mouse_to_click_element(AdminBillLocator.clear_time, doc)
        except:
            pass
        try:
            self.wait_elevisble(AdminBillLocator.wait, doc, timeout=10)
            self.wait_eleinvisble(AdminBillLocator.wait, doc, timeout=10)
        except:
            pass
        self.wait_elevisble(AdminBillLocator.order_time, doc)
        self.click_element(AdminBillLocator.order_time, doc)
        for i in range(12):
            try:
                self.wait_elevisble(AdminBillLocator.select_time(MenuName.select_time[cmdopt]), doc,timeout=10)
                break
            except:
                self.move_mouse_to_click_element(AdminBillLocator.choose_time_before,doc)
                continue
        self.click_element(AdminBillLocator.select_time(MenuName.select_time[cmdopt]), doc)
        self.click_element(AdminBillLocator.select_time(MenuName.select_time[cmdopt]), doc)
        time.sleep(5)
        try:
            self.wait_elevisble(AdminBillLocator.total_text,doc)
            total_text=self.get_element(AdminBillLocator.total_text,doc).text
            # 正则表达式拆分total
            pattern = r"[ ]"  # 模式表示逗号、感叹号、问号或空格出现一次或多次
            total_result = re.split(pattern, total_text)
            total = int(total_result[1])
        except:
            total = 0
        print('根据时间查询页面数量：',total)
        self.save_screen(doc)
        return total

    # 点击下载
    def click_down(self):
        doc = '点击下载'
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.click_download,doc)
        self.move_mouse_to_click_element(AdminBillLocator.click_download,doc)
        self.wait_elevisble(AdminBillLocator.click_down_success_cue,doc)
        self.wait_eleinvisble(AdminBillLocator.click_down_success_cue,doc)
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.export_records_success,doc)


    """
        Export Records 
    """
    # 更新status状态，置空
    def update_export_status(self):
        doc = '更新Export Records页面的status状态为空'
        time.sleep(3)
        self.wait_elevisble(AdminBillLocator.status_choose,doc)
        self.move_mouse_to_click_element(AdminBillLocator.status_choose,doc)
        time.sleep(3)
        if self.is_element_disabled(AdminBillLocator.reset_point):
            self.move_mouse_to_click_element(AdminBillLocator.reset_point, doc)
            time.sleep(3)
            self.wait_elevisble(AdminBillLocator.ok_point, doc)
            self.move_mouse_to_click_element(AdminBillLocator.ok_point, doc)
        else:
            #self.move_mouse_to_click_element(AdminBillLocator.ok_point, doc)
            self.wait_elevisble(AdminBillLocator.ok_point, doc)
            self.move_mouse_to_click_element(AdminBillLocator.ok_point, doc)
        time.sleep(3)

    # 获取列表第一条数据的name，等待第一条数据下载状态变为Export successful，并点击下载
    def download_first_num(self):
        doc = '等待最新数据变为可下载状态，并点击下载'
        time.sleep(2)
        self.update_export_status()
        self.wait_elevisble(AdminBillLocator.first_down_load_data_status,doc)
        self.wait_elevisble(AdminBillLocator.first_down_load_data_name,doc)
        first_down_load_data_name=self.get_element(AdminBillLocator.first_down_load_data_name,doc).text
        self.wait_elevisble(AdminBillLocator.first_download,doc)
        self.move_mouse_to_click_element(AdminBillLocator.first_download,doc)
        time.sleep(20)
        self.save_screen(doc)
        return first_down_load_data_name

    # 判断下载文件的数量和预期是否一致
    def check_download_file(self,cmdopt):
        doc = '获取下载文件并返回文件内容'
        time.sleep(2)
        first_down_load_data_name=self.download_first_num()
        path=MenuName.download_path[cmdopt]
        path = path+first_down_load_data_name
        resultList, resultListlen = excel(path).getExcelData('Transaction No.')
        print("resultList",resultList,"resultListlen",resultListlen)
        return resultList, resultListlen


    """
        Billing Statement
    """
    # 进入Billing Statement页面
    def enter_billing_Statement(self):
        doc = '进入Billing Statement页面'
        time.sleep(5)
        self.scroll_bar_operation(AdminBillLocator.bill_menu)
        text=self.get_attribute(AdminBillLocator.if_open_bill_menu,'Class',doc)
        if text != 'antd4-menu antd4-menu-sub antd4-menu-inline':
            self.move_mouse_to_click_element(AdminBillLocator.bill_menu,doc)
        self.wait_elevisble(AdminBillLocator.billing_statement_Menu,doc)
        self.move_mouse_to_click_element(AdminBillLocator.billing_statement_Menu,doc)
        self.wait_web_page_rotate(time=10)
        self.wait_elevisble(AdminBillLocator.billing_statement_page_text,doc)
        assert self.get_element(AdminBillLocator.billing_statement_page_text,doc).text == 'Billing Statement'

    # 查询列表数据，获取列表所有时间并返回
    def search_list_billing_cycle(self):
        doc = '查询列表数据，获取列表所有时间并返回'
        time.sleep(2)
        list1=[]
        try:
            self.wait_elevisble(AdminBillLocator.billing_cycle_list,doc)
            billing_cycle_list=self.get_elements(AdminBillLocator.billing_cycle_list,doc)
            for i in billing_cycle_list:
                list1.append(i.text)
        except:
            pass
        return list1

    # 查询列表数据，获取列表所有Account Email并返回
    def search_list_account_email(self):
        doc='查询列表数据，获取列表所有account_email并返回'
        time.sleep(2)
        list1 = []
        try:
            self.wait_elevisble(AdminBillLocator.account_email_list, doc)
            billing_cycle_list = self.get_elements(AdminBillLocator.account_email_list, doc)
            for i in billing_cycle_list:
                list1.append(i.text)
        except:
            pass
        return list1

    # 查询列表数据，获取列表所有status并返回
    def search_list_status(self):
        doc='查询列表数据，获取列表所有status并返回'
        time.sleep(2)
        list1 = []
        try:
            self.wait_elevisble(AdminBillLocator.status_list, doc)
            billing_cycle_list = self.get_elements(AdminBillLocator.status_list, doc)
            for i in billing_cycle_list:
                list1.append(i.text)
        except:
            pass
        return list1


    # 交验Billing Cycle选择后查询列表时间是否正确
    def check_billing_cycle(self):
        doc = '选择Billing Cycle查询列表值是否正确'
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.click_billing_cycle,doc)
        self.move_mouse_to_click_element(AdminBillLocator.click_billing_cycle,doc)
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.billing_cycle_item('2'),doc,timeout=10)
        self.move_mouse_to_click_element(AdminBillLocator.billing_cycle_item('2'),doc)
        time_str=self.get_element(AdminBillLocator.billing_cycle_item('2'),doc).text
        self.move_mouse_to_click_element(AdminBillLocator.click_search,doc)
        time.sleep(3)
        billing_cycle_list = self.search_list_billing_cycle()
        all_equal = all(element == time_str for element in billing_cycle_list)
        self.save_screen(doc)
        assert all_equal

    # 交验Account Email选择后查询列表时间是否正确
    def check_account_email(self,cmdopt):
        doc = '选择Account Email查询列表值是否正确'
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.input_account_eamil,doc)
        self.send_keys(AdminBillLocator.input_account_eamil,Logindata.user_data[cmdopt]['user']['username'],doc)
        time.sleep(2)
        self.move_mouse_to_click_element(AdminBillLocator.click_search,doc)
        time.sleep(3)
        account_email_list = self.search_list_account_email()
        all_equal = all(element == Logindata.user_data[cmdopt]['user']['username'] for element in account_email_list)
        self.save_screen(doc)
        assert all_equal

    # 更新Billing Statement状态status，置空
    def update_billing_statement_status(self):
        doc = '更新Billing Statement状态status，置空'
        time.sleep(3)
        self.wait_elevisble(AdminBillLocator.click_status,doc)
        self.move_mouse_to_click_element(AdminBillLocator.click_status,doc)
        time.sleep(3)
        if self.is_element_disabled(AdminBillLocator.status_reset):
            self.move_mouse_to_click_element(AdminBillLocator.status_reset, doc)
            time.sleep(3)
            self.wait_elevisble(AdminBillLocator.status_ok, doc)
            self.move_mouse_to_click_element(AdminBillLocator.status_ok, doc)
        else:
            self.wait_elevisble(AdminBillLocator.status_ok, doc)
            self.move_mouse_to_click_element(AdminBillLocator.status_ok, doc)
        time.sleep(3)

    # 交验status选择后查询列表status是否正确
    def check_select_status(self,status):
        doc = '交验status选择后查询列表status是否正确'
        time.sleep(2)
        self.update_billing_statement_status()
        time.sleep(2)
        self.move_mouse_to_click_element(AdminBillLocator.click_status, doc)
        self.wait_elevisble(AdminBillLocator.choose_status_select(status),doc)
        self.move_mouse_to_click_element(AdminBillLocator.choose_status_select(status),doc)
        self.move_mouse_to_click_element(AdminBillLocator.status_ok, doc)
        self.wait_web_page_rotate(time=5)
        list1=self.search_list_status()
        all_equal = all(element == status for element in list1)
        self.save_screen(doc)
        assert all_equal

    #根据币种查询total值
    def get_num_total_by_settlement_currency(self,cmdopt, get_database_source,currency):
        doc = '根据币种检查Gross，Paid，Outstanding'
        gross_amount, paid_amount, gross_amount_paid_amount = BusinessCommonTools().get_num_total_by_settlement_currency(cmdopt, get_database_source, currency)
        time.sleep(2)
        if currency == 'ZAR':
            self.wait_elevisble(AdminBillLocator.click_search,doc)
            self.move_mouse_to_click_element(AdminBillLocator.click_search,doc)
            time.sleep(2)
        else:
            self.wait_elevisble(AdminBillLocator.click_settlement_currency,doc)
            self.move_mouse_to_click_element(AdminBillLocator.click_settlement_currency,doc)
            time.sleep(1)
            self.wait_elevisble(AdminBillLocator.choose_currency(currency),doc)
            self.move_mouse_to_click_element(AdminBillLocator.choose_currency(currency),doc)
            time.sleep(2)
            self.wait_elevisble(AdminBillLocator.click_search, doc)
            self.move_mouse_to_click_element(AdminBillLocator.click_search, doc)
            time.sleep(2)
        text1 = '实际值:' + str(
            float(self.get_element(AdminBillLocator.total_gross(currency), doc).text.replace(",", ""))) + '预期值:' + str(
            gross_amount)
        assert float(self.get_element(AdminBillLocator.total_gross(currency), doc).text.replace(",", "")) == float(
            gross_amount), text1
        text2 = '实际值：' + self.get_element(AdminBillLocator.total_paid(currency), doc).text + '预期值：' + str(paid_amount)
        assert float(self.get_element(AdminBillLocator.total_paid(currency), doc).text.replace(",", "")) == float(
            paid_amount), text2
        text3 = '实际值：' + self.get_element(AdminBillLocator.total_outstanding(currency), doc).text + '预期值：' + str(
            gross_amount_paid_amount)
        assert float(
            self.get_element(AdminBillLocator.total_outstanding(currency), doc).text.replace(",", "")) == float(
            gross_amount_paid_amount), text3

    # 重置搜索条件
    def refine_search_term(self,cmdopt):
        doc = '重制搜索条件'
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.input_account_eamil, doc)
        self.send_keys(AdminBillLocator.input_account_eamil, Logindata.user_data[cmdopt]['user']['username'], doc)
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.click_search, doc)
        self.move_mouse_to_click_element(AdminBillLocator.click_search, doc)
        assert self.get_attribute(AdminBillLocator.input_account_eamil,'value',doc) == Logindata.user_data[cmdopt]['user']['username']
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.click_reset, doc)
        self.move_mouse_to_click_element(AdminBillLocator.click_reset, doc)
        assert self.get_attribute(AdminBillLocator.input_account_eamil,'value',doc) != Logindata.user_data[cmdopt]['user']['username']

    # 点击下载，返回列表数量
    def click_down_billing_statement(self):
        doc = '点击下载，返回列表数据'
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.click_search, doc)
        self.move_mouse_to_click_element(AdminBillLocator.click_search, doc)
        email_list=self.search_list_account_email()
        self.wait_elevisble(AdminBillLocator.click_down,doc)
        self.move_mouse_to_click_element(AdminBillLocator.click_down,doc)
        time.sleep(5)
        return email_list

    # 检查下载的billing_statement文件详细字段名称是否一致
    def check_down_billing_statement(self,cmdopt):
        doc = '检查billing_statement下载'
        excel_list_title = ['Invoice No.', 'Billing Cycle', 'Account Email', 'Account Type', 'Payment Method', 'Status', 'Gross Amount (ZAR)', 'Paid Amount (ZAR)', 'Outstanding Amount (ZAR)', 'Issue Date (UTC+08:00)']
        email_list=self.click_down_billing_statement()
        time.sleep(2)
        first_down_load_data_name=self.download_first_num()
        path=MenuName.download_path[cmdopt]
        path = path+first_down_load_data_name
        resultList,sheet_num= excel(path).findData(0,'Invoice No.')
        resultList1,sheet_num1= excel(path).getExcelData('Download time')
        text1 = '实际值:'+ ','.join(resultList) + '预期值:'+','.join(excel_list_title)
        text2 = '实际值:'+ str(len(resultList1)-resultList1.index(['Invoice No.'])-1) + '预期值:'+ str(len(email_list))
        assert excel_list_title==resultList,text1
        assert len(resultList1)-resultList1.index(['Invoice No.'])-1 == len(email_list),text2


    # 点击然后时间进行排序 Gross Amount (ZAR)，Paid Amount (ZAR)，Outstanding Amount (ZAR)
    def click_by_time_sequence(self,name,cue,index):
        doc = '时间排序，返回时间字段值'
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.click_search, doc)
        self.move_mouse_to_click_element(AdminBillLocator.click_search, doc)
        for i in range(3):
            time.sleep(3)
            self.move_mouse_to_element(AdminBillLocator.click_search)
            time.sleep(2)
            #self.keyboard_move(AdminBillLocator.bill_body,'100',doc)
            #self.scroll_bar_operation(AdminBillLocator.amount_by_name(name))
            self.move_mouse_to_element(AdminBillLocator.amount_by_name(name))
            try:
                self.wait_elevisble(AdminBillLocator.point_out(cue),doc,timeout=5)
                break
            except:
                self.move_mouse_to_click_element(AdminBillLocator.amount_by_name(name), doc)
                continue
        time.sleep(2)
        self.wait_elevisble(AdminBillLocator.amount_list(str(int(index)+6)),doc)
        amount_list=self.get_elements(AdminBillLocator.amount_list(str(int(index)+6)),doc)
        amount_list_time_float = []
        amount_list_time_str = []
        for i in amount_list:
            amount_list_time_float.append(float(i.text.replace(",", "")))
            amount_list_time_str.append(i.text.replace(",", ""))
        if cue =='Click to sort ascending':
            text = '实际值:'+ ','.join(amount_list_time_str) + '预期：'+'降序'
            assert self.is_descending_order(amount_list_time_float),text
        if cue == 'Click to cancel sorting':
            text = '实际值:' + ','.join(amount_list_time_str) + '预期：' + '升序'
            assert self.is_ascending_order(amount_list_time_float), text


















